package Work4.Slay_the_Maze.Player;

import Work4.Slay_the_Maze.Potions.*;
import Work4.Slay_the_Maze.Cards.*;
import Work4.Slay_the_Maze.Relics.*;
import Work4.Slay_the_Maze.Monsters.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Player {
	private String name;
	private int money;
	private int hp;
	private int hplimit;
	private int mana;
	private int turnmana;
	private int armor;
	private int Restcantreat;
	private ArrayList<Cards> handcards=new ArrayList<Cards>();
	private ArrayList<Cards> battlehandcards=new ArrayList<Cards>();
	private ArrayList<Cards> usedhandcards=new ArrayList<Cards>();
	private ArrayList<Cards> foldhandcards=new ArrayList<Cards>();
	private ArrayList<Relics> myrelics=new ArrayList<Relics>();
	private ArrayList<Potions> mypotions=new ArrayList<Potions>();
	private double attrate = 1;
	private double armorrate = 1;
	private int power=0;
	private int agility=0;
	private final ArrayList<Cards> tempdata=new ArrayList<Cards>();
	public void Player(String name,int hp,int mana) {
		this.name = name;
		this.hp = hp;
		this.mana = mana;
		this.handcards = new ArrayList<Cards>();
		this.myrelics = new ArrayList<Relics>();
		this.mypotions = new ArrayList<Potions>();
	}
	
	public void addmoney(int coin) {
		this.money=this.money+coin;
	}
	public void addhandcard(Cards card) {
		this.handcards.add(card.clone());
		isneedfold();
		this.battlehandcards.add(card.clone());
	}
	public void addrelics(Player player,Relics relic) {
		relic.useit(player);
		this.myrelics.add(relic.clone());
	}
	public void addpotions(Potions potion) {
		this.mypotions.add(potion.clone());
		isneedfold();
	}//获得装备
	
	public void removehandcard(int n) {
		this.handcards.remove(n-1);
	}
	public void removerelics(int n) {
		this.myrelics.remove(n-1);
	}
	public void removepotions(int n) {
		this.mypotions.remove(n-1);
	}//使用装备,丢弃装备
	
	public void Printplayerinfo() {
		System.out.println(name+" 生命值:"+hp+"/"+hplimit+"  护甲值:"+armor+"  法力值:"+mana+" 力量:"+power+" 敏捷:"+agility);
	}
	public void Printhandcard(ArrayList<Cards> handcard) {
		for(int i=0;i<handcard.size();i++) {
			System.out.printf("["+(i+1)+"]  ");
			handcard.get(i).PrintInfo();
		}
	}
	public void Printrelics() {
		for(int i=0;i<myrelics.size();i++) {
			System.out.printf("["+(i+1)+"]  ");
			myrelics.get(i).PrintInfo();
		}
	}
	public void Printpotions() {
		for(int i=0;i<mypotions.size();i++) {
			System.out.printf("["+(i+1)+"]  ");
			mypotions.get(i).PrintInfo();
		}
	}//打印玩家各个信息
	
	
	public void Action(Player player,ArrayList<Monster> monsters) {
		turnmana=mana;
		Scanner sc=new Scanner(System.in);
		boolean temp=false;
		do {
			int choice=0;
			System.out.println("------执行行动------");
			System.out.println("生命值:"+hp+"  护甲值:"+armor+"  法力值:"+turnmana+"/"+mana);
			System.out.println("请选择操作（输入0，1，2、3进行操作）:");
			System.out.println("0.结束回合");
			System.out.println("1.使用卡牌");
			System.out.println("2.使用药水");
			System.out.println("3.休整并结束回合");
			while (!sc.hasNextInt()) {
			    System.out.println("输入无效，请输入一个整数:");
			    sc.next(); // 清除错误输入，避免死循环
			}
			int i=sc.nextInt();
			switch(i){
				case 0:
					temp=true;
					break;
				case 1:
					choice=1;
					break;
				case 2:
					choice=2;
					break;
				case 3:
					setHp(getHp()+Restcantreat);
					ArrayList<Integer> tempindex=new ArrayList<Integer>();
					System.out.println("------------------");
					for(int k=0;k<handcards.size();k++) {
						if(!foldhandcards.contains(handcards.get(k))) {
							System.out.printf("["+(k+1)+"]  ");
							handcards.get(k).PrintInfo();
							tempindex.add(k+1);
						}
					}
					System.out.println("请弃置一张卡牌:");
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请重新输入:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					int cardnum;
					do {
						cardnum=sc.nextInt();
						if(!tempindex.contains(cardnum)) {
							System.out.println("输入无效，请重新输入:");
						}
					}while(!tempindex.contains(cardnum));
					foldhandcards.add(handcards.get(cardnum-1));
					simplerest();
					temp=true;
					break;
				default:
					System.out.println("无效输入");
			}
			if(choice==1) {
				boolean temp0=false;
				do {
					System.out.println("------选择卡牌------");
					System.out.println("生命值:"+hp+"  护甲值:"+armor+"  法力值:"+turnmana+"/"+mana);
					Printhandcard(battlehandcards);
					System.out.println("请选择操作（输入对应数字进行操作，输入0返回）:");
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请输入一个整数:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					i=sc.nextInt();
					if(i==0) {
						temp0=true;
					}else if(i>=1&&i<=10) {
						if(battlehandcards.size()>=i) {
							if(battlehandcards.get(i-1).usemana(player)) {
								battlehandcards.get(i-1).Useit(player, monsters);
								for(int k=monsters.size()-1;k>=0;k--) {
									if(monsters.get(k).getHp()<=0) {
										monsters.remove(k);
									}
								}
								if(monsters.size()==0) {
									temp0=true;
									temp=true;
								}//使用卡牌后插入怪物死亡结算和战斗是否结束结算
								usedhandcards.add(battlehandcards.get(i-1));
								battlehandcards.remove(i-1);
							}
						}else {
							System.out.println("无效输入");
						}
					}else {
						System.out.println("无效输入");
					}
				}while(!temp0);
			}  //使用卡牌
			
			if(choice==2) {
				boolean temp0=false;
				do {
					System.out.println("------选择药水------");
					System.out.println("生命值:"+hp+"  护甲值:"+armor+"  法力值:"+turnmana+"/"+mana);
					Printpotions();
					System.out.println("请选择操作（输入对应数字进行操作，输入0返回）:");
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请输入一个整数:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					i=sc.nextInt();
					while(i>mypotions.size()||i<0) {
						System.out.println("输入无效:");
						i=sc.nextInt();
					}
					if(i>=1&&i<=3) {
						mypotions.get(i-1).Usepotion(player, monsters);
						for(int k=monsters.size()-1;k>=0;k--) {
							if(monsters.get(k).getHp()<=0) {
								monsters.remove(k);
							}
						}
						if(monsters.size()==0) {
							temp0=true;
							temp=true;
						}//使用药水后插入怪物死亡结算和战斗是否结束结算
						mypotions.remove(i-1);
					}else if(i==0) {
						temp0=true;
					}else {
						System.out.println("请选择操作（输入对应数字进行操作，输入0返回）:");
					}
				}while(!temp0);
			} //使用药水
			
		}while(!temp);
	}
	
	public void isneedfold() {
		if(handcards.size()>10) {
			Scanner sc=new Scanner(System.in);
			Printhandcard(handcards);
			System.out.println("卡组上限为10，请移除一张牌:");
			while (!sc.hasNextInt()) {
			    System.out.println("输入无效，请输入一个整数:");
			    sc.next(); // 清除错误输入，避免死循环
			}
			int i=sc.nextInt();
			foldhandcards.remove(handcards.get(i-1));
			handcards.remove(i-1);
		}
	}
	public void isneedfoldpotion() {
		if(mypotions.size()>10) {
			Scanner sc=new Scanner(System.in);
			Printpotions();
			System.out.println("药水上限为3，请移除一瓶药水:");
			int i=sc.nextInt();
			mypotions.remove(i-1);
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		if(hp>this.hplimit) {
			this.hp=hplimit;
			return;
		}
		this.hp = hp;
	}
	public int getMana() {
		return mana;
	}
	public void setMana(int mana) {
		this.mana = mana;
	}
	public ArrayList<Cards> getHandcards() {
		return handcards;
	}
	public void setHandcards(ArrayList<Cards> handcards) {
		this.handcards = handcards;
	}
	public ArrayList<Relics> getMyrelics() {
		return myrelics;
	}
	public void setMyrelics(ArrayList<Relics> myrelics) {
		this.myrelics = myrelics;
	}
	public ArrayList<Potions> getMypotions() {
		return mypotions;
	}
	public void setMypotions(ArrayList<Potions> mypotions) {
		this.mypotions = mypotions;
	}
	public int getArmor() {
		return armor;
	}
	public void setArmor(int armor) {
		this.armor = armor;
	}
	public double getAttrate() {
		return attrate;
	}
	public void setAttrate(double attrate) {
		this.attrate = attrate;
	}
	public double getArmorrate() {
		return armorrate;
	}
	public void setArmorrate(double armorrate) {
		this.armorrate = armorrate;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	public int getAgility() {
		return agility;
	}
	public void setAgility(int agility) {
		this.agility = agility;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	public ArrayList<Cards> getBattlehandcards() {
		return battlehandcards;
	}
	public void setBattlehandcards(ArrayList<Cards> battlehandcards) {
		this.battlehandcards = battlehandcards;
	}
	public ArrayList<Cards> getFoldhandcards() {
		return foldhandcards;
	}
	public void setFoldhandcards(ArrayList<Cards> foldhandcards) {
		this.foldhandcards = foldhandcards;
	}
	public ArrayList<Cards> getUsedhandcards() {
		return usedhandcards;
	}

	public void setUsedhandcards(ArrayList<Cards> usedhandcards) {
		this.usedhandcards = usedhandcards;
	}
	public int getHplimit() {
		return hplimit;
	}

	public void setHplimit(int hplimit) {
		this.hplimit = hplimit;
	}

	public ArrayList<Cards> getTempdata() {
		return tempdata;
	}
	public int getTurnmana() {
		return turnmana;
	}

	public void setTurnmana(int turnmana) {
		this.turnmana = turnmana;
	}
	public int getRestcantreat() {
		return Restcantreat;
	}

	public void setRestcantreat(int restcantreat) {
		Restcantreat = restcantreat;
	}

	public void resetusedcards() {
		usedhandcards.clear();
	}
	public void resetfoldcards() {
		foldhandcards.clear();
	}
	public void simplerest() {
		ArrayList<Cards> tempcards=new ArrayList<Cards>();
		for(int i=0;i<handcards.size();i++) {
			if(!foldhandcards.contains(handcards.get(i))){
				tempcards.add(handcards.get(i));
			}
		}
		battlehandcards = new ArrayList<Cards>(tempcards);
		resetusedcards();
	}
	public void damage(int damage) {
		if(armor>0) {
			if(armor>=damage) {
				armor=armor-damage;
				return;
			}else if(armor<damage) {
				damage=damage-armor;
				armor=0;
			}
		}
		hp=hp-damage;
	}

	public Player() {
		name="故障机器人";
		money=30;
		hp=50;
		hplimit=50;
		mana=5;
		armor=0;
		Restcantreat=0;
		handcards.add(new attack());
		handcards.add(new attack());
		handcards.add(new defense());
		handcards.add(new defense());
		battlehandcards = new ArrayList<Cards>(handcards); 
	}
	public void mybag() {
		Scanner sc=new Scanner(System.in);
		System.out.println("请选择:");
		System.out.println("0.离开");
		System.out.println("1.查看牌库");
		System.out.println("2.查看药水");
		System.out.println("3.查看遗物");
		System.out.println("4.查看属性");
		int choice;
		boolean temp=false;
		do {
			choice=sc.nextInt();
			switch(choice) {
			case 0:
				temp=true;
				break;
			case 1:
				System.out.printf("牌库:");
				Printhandcard(handcards);
				//System.out.printf("战斗手牌:");
				//Printhandcard(battlehandcards);
				//System.out.printf("使用牌:");
				//Printhandcard(usedhandcards);
				System.out.println("继续");
				break;
			case 2:
				Printpotions();
				System.out.println("继续");
				break;
			case 3:
				Printrelics();
				System.out.println("继续");
				break;
			case 4:
				System.out.printf("金币:"+money+" ");
				Printplayerinfo();
				System.out.println("继续");
				break;
			default:
				System.out.println("无效输出");
			}
		}while(!temp);
	}
}
